<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title>
    <link rel="stylesheet" href="css/list.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <style>
        
        .nav{
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            height: 50px;
        }
        .container{
            padding-top: 80px;
            height: auto;
        }
        .container-right{
            width: auto;
        }
        .blog-pagnation-wrapper{
            height: 40px;
            margin: 16px 0;
            text-align: center;
        }
        .blog-pagnation-item{
            display: inline-block;
            padding: 8px;
            border: 1px solid #d0d0d5;
            color: #333;
        }
        .blog-pagnation-item:hover{
            background: #4e4eeb;
            color: #fff;
        }
        .blog-pagnation-item.actvie{
            background: #4e4eeb;
            color: #fff;
        }

    </style>

    <script src="js/jquery.min.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo2.jpg" alt="">
        <span class="title">博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="blog_edit.html">写博客</a>
        <a href="javascript:onExit()">退出登录</a>
        <!-- <a href="#">注销</a> -->
    </div>
    <!-- 版心 -->
    <div class="container">
        <div class="container-left" >
            <div class="card">
                <img src="img/doge.jpg" class="avtar" alt="">
                <h3 id="username"></h3>
                <a href="http:www.gitee.com">Gitee</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span id="count"></span>
                    <span>1</span>
                </div>
            </div>
        </div>
        <div class="container-right" id="artListDiv">
            
        </div>
    </div>
</body>
<script>
    var articleinfo = null ;

    function myCount() {
        jQuery.ajax({
            url:"/art/count",
            type:"GET",
            data:{},
            success: function(body) {
                console.log(body);
                if (body.code == 200 && body.data != null) {
                    jQuery("#count").text(body.data);
                }
            },
            error(body) {
                if (body != null && body.status == 401) {
                alert("未登录，即将跳转到登陆页面");
                location.href = "/login.html";
                }
            }
        })
    }
    myCount();

    function myInfo() {
        jQuery.ajax({
            url:"/user/info",
            type:"POST",
            data:{},
            success: function(body) {
                console.log(body);
                if (body.code == 200 && body.data != null) {
                    jQuery("#username").text(body.data.username);
                }
            },
            error(body) {
                if (body != null && body.status == 401) {
                alert("未登录，即将跳转到登陆页面");
                location.href = "/login.html";
                }
            }
        })
    }
    myInfo();

    function subString(content) {
        if (content.length > 85) {
            return content.substr(0,85);
        }
        return content;
    }

    function initList() {
        jQuery.ajax({
            url:"/art/list",
            type:"GET",
            data:{},
            success:function(body){
                if (body.code == 200 
                && body.data != null 
                && body.data.length > 0) {
                    console.log(body);

                    //todo:发表文章内容 
                    var html = "";
                    body.data.forEach(function(item) {
                        html += '<div class="blog">' +
                            '<div class="title">' + item.title + '</div>' +
                            '<div class="date">' + item.createtime +'</div>' +
                            '<div class="desc">' + subString(item.content) + '</div>' + 
                            '<div style="text-align: center;margin-top: 50px;">' + 
                            '<a href="blog_content.html?id=' + item.id + '">查看全文</a>&nbsp;&nbsp;' + 
                            '<a href="blog_update.html?id=' + item.id + '">编辑</a>&nbsp;&nbsp;' +
                            '<a href="javascript:myDel(' + item.id + ')">删除</a></div>' +
                            '</div>'
                    });
                    // 拼接
                    jQuery("#artListDiv").html(html);
                } else {
                    jQuery("#artListDiv").html("<h1>暂无数据</h1>");
                }
            },
            error: function(body) {
                // alert("未登录，即将跳转到登陆页面");
                location.href = "/login.html";
            }
        })
    }
    initList();

    function onExit() {
        // confirm弹出一个选择框 选择确认就会返回true
        if (confirm("真的要退出么？")) {
            jQuery.ajax({
                url:"/user/logout",
                type:"POST",
                data:{},
                success: function(body) {
                    location.href = "/login.html";
                },
                error: function(body) {
                    if (body != null && body.status == 401) {
                        alert("未登录");
                        location.href = "/login.html";
                    }
                }
            }) 
        }
    }

    function myDel(aid) {
        if (confirm("确定要删除文章？")) {
            if (confirm("万劫不复的喔")) {
                jQuery.ajax({
                    url:"/art/del",
                    type:"POST",
                    data:{
                        "aid":aid
                    },
                    success: function(body) {
                            alert("删除成功！");
                            // todo: 调整成局部刷新而不是整个页面刷新
                            location.href = "/blog_list.html";
                        },
                    error: function() {
                        alert("删除失败，刷新再试试吧");
                    }
                })
            }
        }
    }
    
    // 获取URL中queryString参数
    function getURLParam(key){
        // 通过location.search拿到URL
        var params = location.search;
        // 有query string就进入下一步
        if(params.indexOf("?")>=0){
        //    获得query string
        params = params.substring(params.indexOf("?") + 1);
            //获得键值对集合 
        var paramArr = params.split('&');
            //遍历键值对集合 
        for(var i=0;i<paramArr.length;i++){
                // 分割键和值
                var namevalues = paramArr[i].split("=");
                // key是函数的参数 和参数匹配就返回对应的value
                if (namevalues[0]==key) {
                    return namevalues[1];
                }
            }
        } else {
            return "";
        }
    }
</script>
</html>